package com.jd.finance.oss.controller;


import com.aliyuncs.CommonResponse;
import com.jd.finance.oss.apiClient.UserClient;
import com.jd.finance.oss.exception.OSSException;
import com.jd.finance.oss.model.OssObject;
import com.jd.finance.oss.model.User;
import com.jd.finance.oss.service.FileService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.result.ResponseEnum;
import com.netflix.ribbon.proxy.annotation.Content;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@Api(tags = "阿里云文件管理")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/api/oss")
public class FileController {

    @Resource
    private FileService fileService;

    @Resource
    private UserClient userClient;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public BaseResult upload(MultipartFile[] file, String module, String uid) {

        if (file == null || file.length == 0) {
            return BaseResult.error().message("上传失败，文件为空");
        }

        if (StringUtils.isBlank(module)) {
            return BaseResult.error().message("上传失败，所属模块为空");
        }
        if (StringUtils.isBlank(uid)) {
            return BaseResult.error().message("上传失败，uid为空");
        }
        User userInfoS = userClient.getUserInfoS(Integer.parseInt(uid));
        if (userInfoS == null) {
            userInfoS = new User();
            userInfoS.setUid(null);
            userInfoS.setUserName(uid);
        }
        try {
            List<OssObject> ossObjects = fileService.upload(file, module, userInfoS);
            return BaseResult.ok().data("list", ossObjects).message("上传成功");
        } catch (OSSException e) {
            return BaseResult.setResult(ResponseEnum.UPLOAD_ERROR);
        }
    }

    @ApiOperation(value = "对象下载")
    @RequestMapping(value = "/objects/v1/download",
            produces = { "application/json" },
            method = RequestMethod.GET)
    public BaseResult downloadObjects(Long objectId, HttpServletRequest req, HttpServletResponse res) {

        if (objectId == null) {
            return BaseResult.error().message("下载失败，参数objectId为空");
        }

        try {
            fileService.downloadObject(objectId, req, res);
        } catch (Exception e) {
            //设置返回报文类型
            res.setContentType("application/json");
            log.error("下载失败", e);
            return BaseResult.setResult(ResponseEnum.SYS_EXCEPTION);
        }
        return BaseResult.ok();
    }


    @ApiOperation("删除oss文件")
    @DeleteMapping("/remove")
    public BaseResult remove(
            @ApiParam(value = "要删除的文件", required = true)
                    String url) {
        fileService.removeFile(url);
        return BaseResult.ok().message("删除成功");
    }
}
